# -*- coding: utf-8 -*-
# Create your views here.

from django.http import HttpResponse
from django.template.loader import get_template#, Context
from django.template import RequestContext, Context

from django.views.decorators.csrf import csrf_exempt

import json, simplejson

from grammar import Verb
import grammar

from models import Lesson, Document, Drill, Evaluator, Question, User, QuestionEvent


def welcome(request):
  t = get_template('welcome.html')
  c = RequestContext(request)
  c['lessons'] = Lesson.objects.all()
  return HttpResponse(t.render(c))

def lessonplan(request):
  t = get_template('drill/lessonplan.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def tutorial(request):
  t = get_template('drill/tutorial.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def drill(request):
  t = get_template('drill/drill.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def drill2(request):
  t = get_template('drill/drill2.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def drill3(request):
  t = get_template('drill/drill3.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def text(request):
  t = get_template('drill/text.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def download(request):
  t = get_template('drill/download.html')
  c = Context({'request':request})
  return HttpResponse(t.render(c))

def foo(request, lesson_sname, drill_sname):
  lesson = Lesson.objects.get(sname=lesson_sname)
  drill = Drill.objects.get(sname=drill_sname, lesson=lesson)
  t = get_template('drill/foo.html')
  c = RequestContext(request, {'request':request})
  c['lesson'] = lesson
  c['drill'] = drill
  return HttpResponse(t.render(c))

def show_document(request, lesson_sname, drill_sname):
  lesson = Lesson.objects.get(sname=lesson_sname)
  document = Document.objects.get(sname=drill_sname, lesson=lesson)
  t = get_template('drill/show_document.html')
  c = RequestContext(request, {'request':request})
  c['lesson'] = lesson
  c['document'] = document
  return HttpResponse(t.render(c))
  
def drill_list(request, lesson_sname):
  lesson = Lesson.objects.get(sname=lesson_sname)
  t = get_template('drill/drill_list.html')
  c = RequestContext(request, {'request':request})
  c['lesson'] = lesson
  return HttpResponse(t.render(c))
  
def details(request, lesson_sname):
  t = get_template('drill/details.html')
  c = RequestContext(request, {'request':request})
  c['lesson'] = Lesson.objects.get(sname=lesson_sname)
  return HttpResponse(t.render(c))


@csrf_exempt
def json_get_question_simple(request):
  i = int(request.GET['i'])
  drill_sname = request.GET['drill']
  drill = Drill.objects.get(sname=drill_sname)
  drilldata = drill.data
  questions = eval(drill.data.replace('\r\n',''))
  if i < len(questions):
    responsedata = questions[i]
  else:
    responsedata = {'no more questions':'no more'}
  jsondata = simplejson.dumps(responsedata)
  return HttpResponse(jsondata, content_type='text/plain')

@csrf_exempt
def json_get_question1(request):
  i = int(request.GET['i'])
  if i < len(QUESTIONS):
    responsedata = QUESTIONS[i]
  else:
    responsedata = {'no more questions':'no more'}
  jsondata = simplejson.dumps(responsedata)
  return HttpResponse(jsondata, content_type='text/plain')


@csrf_exempt
def json_evaluate_answer1(request):
  data = simplejson.loads(request.raw_post_data, encoding='utf-8')
  v = Verb(data['verb'])
  tense = 'present'
  grampers = data['grampers']
  responsedata = v.evaluate(tense, grampers, data['answer'])
  responsedata['users_answer'] = data['answer']
  PARTS_TMPL = '''<table class='parts'><tr>%s</tr></table>'''
  PART_TMPL = '''<th>%s<br><span style="font-size:8pt;"> %s </span></th>'''
  x = [PART_TMPL % (part[1],part[2]) for part in responsedata['parts']]
  responsedata['partshtml'] = PARTS_TMPL % '<td>+</td>'.join(x)
  
  jsondata = simplejson.dumps(responsedata)
  return HttpResponse(jsondata, content_type='text/plain')

def json_get_question2(request,lesson_sname, drill_sname):
  #data = simplejson.loads(request.raw_post_data)
  #v = Verb(data['verb'])
  #tense = 'present'
  #grampers = data['grampers']
  #responsedata = v.evaluate(tense, grampers, data['answer'])
  #responsedata['users_answer'] = data['answer']
  #PARTS_TMPL = '''<table class='parts'><tr>%s</tr></table>'''
  #PART_TMPL = '''<th>%s<br><span style="font-size:8pt;"> %s </span></th>'''
  #x = [PART_TMPL % (part[1],part[2]) for part in responsedata['parts']]
  #responsedata['partshtml'] = PARTS_TMPL % '<td>+</td>'.join(x)

  #q = {
    #'lesson_sname':'lesson_02',
    #'drill_sname':'lesson_02',
    #'question_text':'What is his name?',
    #'evaluator_sname':'verbs_simple_present_sense_conjugation',
    #'inputs':[
        #{
          #'type':'text',
          #'name':'Conjugated',
          #'id':'conjugated',
        #},
        ##{
          ##'type':'text',
          ##'name':'Suffix',
          ##'id':'suffix',
        ##},
      #],
  #}
  #q = grammar.get_question__verbs_simple_present_sense_conjugation()
  #q_json = simplejson.dumps(q)
  q_json = '{"hej":3, "fix":6, "mus":[2,3,4]}';    
  return HttpResponse(q_json, content_type='text/plain')



@csrf_exempt
def json_evaluate_answer2(request):
  q = simplejson.loads(request.raw_post_data)
  evaluator_sname = q['evaluator_sname']
  print evaluator_sname
  #evaluator = Evaluator.objects.get(sname = evaluator_sname)
  evaluator = grammar.get_evaluator(evaluator_sname)
  #q = evaluator(q)
  #print q
  #v = Verb(data['verb'])
  #tense = 'present'
  #grampers = data['grampers']
  #responsedata = v.evaluate(tense, grampers, data['answer'])
  #responsedata['users_answer'] = data['answer']
  #PARTS_TMPL = '''<table class='parts'><tr>%s</tr></table>'''
  #PART_TMPL = '''<th>%s<br><span style="font-size:8pt;"> %s </span></th>'''
  #x = [PART_TMPL % (part[1],part[2]) for part in responsedata['parts']]
  #responsedata['partshtml'] = PARTS_TMPL % '<td>+</td>'.join(x)
  
  q_json = simplejson.dumps(q)
  return HttpResponse(q_json, content_type='text/plain')


